<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>添加文章</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="/public/static/admin/css/oksub.css">
<!--	<script type="text/javascript" src="/public/static/admin/lib/loading/okLoading.js"></script>-->
</head>
<body>
<div class="ok-body">
	<!--form表单-->
	<form class="layui-form layui-form-pane ok-form">
		<div class="layui-form-item">
			<label class="layui-form-label">标题</label>
			<div class="layui-input-block">
				<input type="text" name="title" placeholder="请输入文章标题" autocomplete="off" class="layui-input"
				       lay-verify="required">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">分类</label>
			<div class="layui-input-block">
				<select name="classify" id="classify">

				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">标签</label>
			<div class="layui-input-block">
				<input type="text" name="label" placeholder="请输入文章标签" autocomplete="off" class="layui-input"
				       lay-verify="required">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-block">
				<input type="checkbox" name="status" lay-skin="switch" lay-text="启用|停用" checked value="0">
				<input type="hidden" id="text1" name="text1" value="0000" >
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">置顶</label>
			<div class="layui-input-block">
				<input type="checkbox" name="isTop" lay-skin="switch" lay-text="是|否" value="0">
			</div>
		</div>




		<button type="button" class="layui-btn" id="cover">
			<i class="layui-icon">&#xe67c;</i>上传缩略图
		</button>
		<img id="preview" name="preview" width="200px" height="200px">
		<input type="text" hidden="hidden"  value="" name="imagesrc" id="imagesrc1"/>

		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">摘要</label>
			<div class="layui-input-block">
				<textarea name="des" placeholder="请输入文章内容" class="layui-textarea" lay-verify="required"></textarea>
			</div>
		</div>
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">内容</label>
			<div id="editor">

			</div>
			<br/>
<!--			<button type="button" class="layui-btn" onclick="setContent()">设置内容</button>-->
<!--			<button type="button" class="layui-btn" onclick="appendContent()">追加内容</button>-->
<!--			<button type="button" class="layui-btn" onclick="clearContent()">清空内容</button>-->
<!--			<button type="button" class="layui-btn" onclick="getHtmlContent()">获取HTML内容</button>-->
<!--			<button type="button" class="layui-btn" onclick="getTextContent()">获取TEXT内容</button>-->
		</div>
		<!--js逻辑-->
		<script src="/public/static/admin/lib/wangEditor/release/wangEditor.min.js"></script>
		<script type="text/javascript">
			let E = window.wangEditor;
			let editor = new E("#editor");
			var text1=document.getElementById('text1');
			editor.customConfig.uploadImgShowBase64 = true   // 使用 base64 保存图片

			editor.customConfig.onchange=function(html){
				text1.setAttribute('value',editor.txt.html());
			}
			editor.customConfig.uploadImgServer = '../../admin/Often/editUploads'; //上传URL
			editor.customConfig.uploadImgMaxLength = 100;
			editor.customConfig.uploadImgMaxSize = 2 * 600 * 600;
			editor.customConfig.uploadFileName = 'file';//服务端接收file的名字
			editor.customConfig.uploadImgHooks = {
				customInsert: function (insertImg, result, editor) {
					// 图片上传并返回结果，自定义插入图片的事件（而不是编辑器自动插入图片！！！）
					// insertImg 是插入图片的函数，editor 是编辑器对象，result 是服务器端返回的结果
					console.log(result);
					// 举例：假如上传图片成功后，服务器端返回的是 {url:'....'} 这种格式，即可这样插入图片：
					var url =result.data;
					insertImg(url);

					// result 必须是一个 JSON 格式字符串！！！否则报错
				}
			}
			editor.customConfig.debug = true;
			editor.create();


		</script>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="addArticle">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
<!--js逻辑-->
<script src="/public/static/admin/lib/layui/layui.js"></script>
<script src="/public/static/admin/js/jquery-3.3.1.js"></script>
<script>
	layui.use(["form", "okUtils", "okLayer"], function () {
		let form = layui.form;
		let okUtils = layui.okUtils;
		let okLayer = layui.okLayer;
		let okMock = layui.okMock;
		let htmls="";
		// okLoading.close();
		okUtils.ajax("often/selectServer", "post", null, true).done(function (response) {
			resultData = response.data;
			for(var x in resultData){
				htmls += '<option value = "' + resultData[x].cate_id + '">' + resultData[x].cate_name + '</option>'
			}
			$("#classify").html(htmls);
			//需要重新渲染
			layui.use('form', function() {
				var form = layui.form;
				form.render();
			});
		});

		form.on("submit(addArticle)", function (data) {

			okUtils.ajax("often/addArticle", "post", data.field, true).done(function (response) {
				okLayer.greenTickMsg(response.msg, function () {
					parent.layer.close(parent.layer.getFrameIndex(window.name));
				});
			}).fail(function (error) {
				console.log(error)
			});
			return false;
		});




	});



	layui.use('upload', function() {
		var upload = layui.upload;
		var $ = layui.jquery;
		var uploadInst = upload.render({
			elem:'#cover'
			,url:'../../admin/Often/upload'
			,accept:'file'  // 允许上传的文件类型
			,auto:true // 自动上传
			,before:function (obj) {
				console.log(obj);
				// 预览
				obj.preview(function(index,file,result) {
					// console.log(file.name);    //图片名字
					// console.log(file.type);    //图片格式
					// console.log(file.size);    //图片大小
					console.log(result);       //图片地址
					$('#preview').attr('src',result); //图片链接 base64
					$('#imagesrc1').attr('value',result);
				});
				// layer.load();
			}
			// 上传成功回调
			,done:function(res) {
				// console.log(upload);

				$('#imagesrc1').attr('value',res['url']);
				if (res.code == 0) {
					return layer.msg(res.msg);
				}
				return layer.msg(res.msg);
			}
			// 上传失败回调
			,error:function(index,upload) {
				return layer.msg(res.msg);
			}

		});
	});




</script>
</body>
</html>
